//
//  LRXWithdrawController.m
//  路人侠
//
//  Created by zhangxulong on 5/28/16.
//  Copyright © 2016 zhangxulong. All rights reserved.
//

#import "LRXWithdrawController.h"
#import "LRXPocketLoadData.h"
#import "LRXRandomString.h"
@interface LRXWithdrawController ()<UITextFieldDelegate>
@property (nonatomic,weak) UITextField * moneyText;
@end

@implementation LRXWithdrawController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:240 green:240 blue:240];
    self.navigationItem.title = @"提现";
    
    UIView * contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 36 * hScale, self.view.frame.size.width, 324 * hScale)];
    contentView.backgroundColor = [UIColor whiteColor];
    
    UILabel * pp = [[UILabel alloc]initWithFrame:CGRectMake(32 * wScale, 34 * hScale, 0, 0)];
    pp.text = @"当前绑定支付宝账号";
    pp.font = [UIFont systemFontOfSize:27 * hScale];
    [pp sizeToFit];
    
    CGFloat  zfbAcountX = CGRectGetMaxX(pp.frame) + 20 * wScale;
    UILabel * zfbAcountLabel = [[UILabel alloc]initWithFrame: CGRectMake(zfbAcountX, CGRectGetMinY(pp.frame), self.view.frame.size.width - zfbAcountX - 30 * wScale, 30 * hScale)];
    zfbAcountLabel.textColor = [UIColor colorWithRed:133 green:133 blue:133];
    zfbAcountLabel.font = [UIFont systemFontOfSize:27 * hScale];
    zfbAcountLabel.textAlignment = NSTextAlignmentRight;
    if (self.zfbAcount == nil) {
        zfbAcountLabel.text = @"(未绑定)";
    }
    else{
        zfbAcountLabel.text = self.zfbAcount;
    }

    
    
    UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(32 * wScale, CGRectGetMaxY(pp.frame) + 10 * hScale, self.view.frame.size.width - 32 * wScale, 2 * hScale)];
    lineView.backgroundColor = GraylineColor;
    
    UILabel * money = [[UILabel alloc]initWithFrame:CGRectMake(32 * wScale, CGRectGetMaxY(lineView.frame) + 23 * hScale, 0, 0)];
    money.text = @"提现金额";
    money.font = [UIFont systemFontOfSize:25 * hScale];
    [money sizeToFit];
    
    UILabel * moneyp = [[UILabel alloc]initWithFrame:CGRectMake(80 * wScale, CGRectGetMaxY(money.frame) + 53 * hScale, 0, 0)];
    moneyp.text = @"¥";
    moneyp.font = [UIFont systemFontOfSize:34 * hScale];
    [moneyp sizeToFit];
    
    
    UITextField * moneyText = [[UITextField alloc]initWithFrame:CGRectMake(146 * hScale, CGRectGetMaxY(moneyp.frame) - 40 * hScale, 200 * wScale, 35 * hScale)];
    moneyText.placeholder = @"输入提取金额";
    moneyText.font = [UIFont systemFontOfSize:31 * hScale];
    moneyText.delegate = self;
    self.moneyText = moneyText;
    
    UIView * lineView2 = [[UIView alloc]initWithFrame:CGRectMake(32 * wScale, CGRectGetMaxY(moneyText.frame) + 40 * hScale, self.view.frame.size.width - 32 * wScale, 2 * hScale)];
    lineView2.backgroundColor = GraylineColor;
    
    NSString * ooStr = [NSString stringWithFormat:@"当前钱包余额%@元", self.balance];
    UILabel * oo = [[UILabel alloc]initWithFrame:CGRectMake(32 * wScale, CGRectGetMaxY(lineView2.frame) + 20 * hScale, 0, 0)];
    oo.text  =  ooStr;
    oo.font = [UIFont systemFontOfSize:23 * hScale];
    oo.textColor = [UIColor colorWithRed:133 green:133 blue:133];
    [oo sizeToFit];
    
    
    UIButton * accp = [UIButton buttonWithType:UIButtonTypeCustom];
    [accp setTitle:@"全部提取" forState:UIControlStateNormal];
    accp.titleLabel.font = [UIFont systemFontOfSize:24 * hScale];
    [accp setTitleColor:mainColor forState:UIControlStateNormal];
    accp.frame = CGRectMake(self.view.frame.size.width - 130 * wScale, 20 * hScale + CGRectGetMaxY(lineView2.frame), 100 * wScale, 25 * hScale);
    [accp addTarget:self action:@selector(accpClick) forControlEvents:UIControlEventTouchUpInside];
    
    [contentView addSubview:zfbAcountLabel];
    [contentView addSubview:accp];
    [contentView addSubview:oo];
    [contentView addSubview:lineView2];
    [contentView addSubview:moneyText];
    [contentView addSubview:moneyp];
    [contentView addSubview:pp];
    [contentView addSubview:lineView];
    [contentView addSubview:money];
    
    
    UIButton * uu = [UIButton buttonWithType:UIButtonTypeCustom];
    [uu setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [uu setTitle:@"确认提取" forState:UIControlStateNormal];
    uu.backgroundColor = mainColor;
    uu.titleLabel.font = [UIFont systemFontOfSize:32 * hScale];
    uu.layer.cornerRadius = 10 * hScale;
    uu.frame = CGRectMake(75 * wScale, CGRectGetMaxY(contentView.frame) + 66 * hScale, self.view.frame.size.width - 150 * wScale,80 * hScale);
    [uu addTarget:self action:@selector(uuClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:uu];
    [self.view addSubview:contentView];
}
- (void)uuClick
{
    
    NSString * strA = [LRXRandomString RandomStringWithLenght:2 withModel:Alphanumeric];
    
    strA = [LRXBlowFish enCodeBlowfishWithValue:strA withKeyStr:blowFishKey];
    strA = [strA stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    NSString * strB = @"55";
    strB = [LRXBlowFish enCodeBlowfishWithValue:strB withKeyStr:blowFishKey];
    strB = [strB stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    NSString * strC = [LRXRandomString RandomStringWithLenght:16 withModel:digitalCharacterMix];
    
    strC = [LRXBlowFish enCodeBlowfishWithValue:strC withKeyStr:blowFishKey];
    strC = [strC stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    NSString * strD = [LRXRandomString RandomStringWithLenght:7 withModel:Alphanumeric];
    
    strD = [LRXBlowFish enCodeBlowfishWithValue:strD withKeyStr:blowFishKey];
    strD = [strD stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    NSString * strE = registrationID;
    
    strE = [LRXBlowFish enCodeBlowfishWithValue:strE withKeyStr:blowFishKey];
    strE = [strE stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    NSDictionary * parma = @{@"A":strA,@"B":strB,@"C":strC,@"D":strD,@"E":strE};

    pocketLoadDataBlock loadDataBlock = ^(NSDictionary * response){
        NSInteger statusCode = [response[@"data"][@"status_code"] integerValue];
        if (statusCode == 6) {
            UIAlertController * alertController = [UIAlertController alertControllerWithTitle:nil message:@"抱歉，无法完成提现，余额不足。" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
            [alertController addAction:okAction];
            [self presentViewController:alertController animated:YES completion:nil];
            
        }
        else if(statusCode == 1)
        {
            [MBProgressHUD showSuccess:@"提现成功"];
            self.moneyText.text = @"";
        }
        //init first
        else if(statusCode == 5)
        {
            [MBProgressHUD showError:@"没有钱包"];
        }
    };
    LRXPocketLoadData * session = [[LRXPocketLoadData alloc]initWithLoadDataBlock:loadDataBlock];
    [session sessionDataTask:parma withUrl:pockerRaUrl withMethod:@"POST"];
    
}
- (void)accpClick{
    self.moneyText.text = self.balance;
    
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.view endEditing:YES];
    return YES;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


@end
